# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd

# 自定义聚合函数
def peak_range(df):
    return  df.max()-df.min()
if __name__ == '__main__':
    dict_obj = {
        'key1':['a', 'b', 'a', 'b','a', 'b', 'a', 'a'],
        'key2':['one', 'one', 'two','three','two','two','one','three'],
        'data1':np.random.randint(1,8, 8),
        'data2':np.random.randint(1,8, 8)
    }
    frame = pd.DataFrame(dict_obj)
    print frame
    print frame.groupby('key1').agg(peak_range)
    print frame.groupby('key1').agg(lambda x : x.max()-x.min())
    print "================同时应用多个聚合函数======================="
    print frame.groupby('key1').agg(['mean', 'std', 'count', peak_range])